﻿using Common.Regexs;
using Common.Utils;
using Entity.Models.dto;
using Entity.Models.Entities;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using RepositorySys.Filte;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using utils;
using utils.enums;
using utils.EX;
using Utils;

namespace RepositorySys.Controllers
{
    /// <summary>
    /// 角色管理控制器
    /// </summary>
    [CustomActionFilte]
    public class RoleInfoController : Controller
    {
        //角色
        IRoleInfoBLL _roleBLL;

        //用户
        IUserBLL _iUserBLL;

        //菜单
        IMenuInfoBLL _iMenuInfoBLL;

        public RoleInfoController(IRoleInfoBLL roleBLL, IUserBLL IUserBLL, IMenuInfoBLL iMenuInfoBLL)
        {
            this._roleBLL = roleBLL;
            this._iUserBLL = IUserBLL;
            this._iMenuInfoBLL = iMenuInfoBLL;
        }

        #region 角色视图

        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddRoleView()
        {
            return View();
        }

        /// <summary>
        /// 修改视图
        /// </summary>
        /// <returns></returns>
        public IActionResult EditRoleView()
        {
            return View();
        }
        /// <summary>
        /// 角色绑定视图
        /// </summary>
        /// <returns></returns>
        public IActionResult BinUserView()
        {
            return View();
        }

        /// <summary>
        /// 菜单绑定视图
        /// </summary>
        /// <returns></returns>
        public IActionResult BinMenuView()
        {
            return View();
        }

        #endregion

        #region 绑定菜单

        /// <summary>
        /// 穿梭框数据列表(菜单)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getBinMenu_Role(string roleId)
        {
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.SUCCESS), new
            {
                //备选菜单
                Menu = _iMenuInfoBLL.getMenuInfoSpinner(),
                //当前角色下已选菜单
                SelectedMenu = _roleBLL.getSelectedMenu(roleId),
            }));
        }

        /// <summary>
        ///绑定菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BinMenu_Role(List<String> menuId, string roleId)
        {
            object data = null;
            try
            {
                data = _roleBLL.BinMenu_Role(menuId, roleId);
            }
            catch (MyException e)
            {
                return getException(data, e);
            }
            return Json(new JsonResults(200, "绑定菜单成功", data));
        }
        #endregion

        #region 绑定用户

        /// <summary>
        /// 穿梭框数据列表（用户）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getBinUser_Role(string roleId)
        {
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.SUCCESS), new
            {
                //备选用户
                User = _iUserBLL.getLeader(null),
                //当前角色下已选用户
                SelectedUser = _roleBLL.getSelectedUser(roleId),
            }));
        }

        /// <summary>
        ///绑定角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BinUser_Role(List<String> userId, string roleId)
        {
            object data = null;
            try
            {
                data = _roleBLL.BinUser_Role(userId, roleId);
            }
            catch (MyException e)
            {
                 return getException(data, e);
            }
            return Json(new JsonResults(200, "绑定用户成功", data));
        }

        #endregion

        #region 增删改查
        /// <summary>
        ///添加角色用户信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRoleInfo(RoleInfo roleInfo)
        {
            object data = null;
            string mgs = null;
            if (Verify(roleInfo,out mgs))
            {
                try
                {
                    data = _roleBLL.AddRoleInfo(roleInfo);
                }
                catch (MyException e)
                {
                    return getException(data, e);
                }
            }
            return Json(new JsonResults(200, mgs==null?Enums.getDescribe(UserEnums.USER_STATE.SUCCESS):mgs, data));
        }

        /// <summary>
        /// 角色信息列表、搜索
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetRoleInfoShow(string roleName, string page, string limit)
        {
            int cont;
            List<RoleInfoDTO> roleInfos = _roleBLL.getRoleShow(roleName, page, limit, out cont);
            return Json(new JsonResults(0, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), cont, roleInfos));
        }

        /// <summary>
        /// 查询角色信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public IActionResult getbyRoleInfoId(string roleId)
        {
            return Json(new JsonResults(0, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), 1000, _roleBLL.getbyRoleInfoId(roleId)));
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="role">用户信息</param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateRoleInfo(RoleInfo role)
        {
            object data = null;
            string mgs = null;
            if (Verify(role, out mgs))
            { 
                try
            {
                data = _roleBLL.UpdateRoleInfo(role);
            }
            catch (MyException e)
            {
                //返回异常信息
                 return getException(data, e);
            }
            }
            return Json(new JsonResults(200, mgs==null? Enums.getDescribe(UserEnums.USER_STATE.UPDATE):mgs, data));
        }

        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DatateRoleInfo(string roleId)
        {
            object data = null;
            try
            {
                data = _roleBLL.DatateRoleInfo(roleId);
            }
            catch (MyException e)
            {
                 return getException(data, e);
            }
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.DATATE), data));
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DatateRoleInfos(List<String> roleIds)
        {
            object data = null;
            try
            {
                data = _roleBLL.DatateRoleInfos(roleIds);
            }
            catch (MyException e)
            {
                 return getException(data, e);
            }
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.DATATE), data));
        }

        #endregion
        /// <summary>
        /// 参数验证
        /// </summary>
        /// <param name="department">参数</param>
        /// <returns></returns>
        private bool Verify(RoleInfo roleInfo, out string mgs)
        {
            if (!new Regex(Regular.Name).Match(roleInfo.RoleName).Success)
            {
                mgs = "请输入正确的角色名称";
                return false;
            }
            else if (roleInfo.Description!=null?!new Regex(Regular.Description).Match(roleInfo.Description).Success:true)
            {
                mgs = "请输入 2-36 内的字符的描述信息";
                return false;
            }
            mgs = null;
            return true;
        }
        /// <summary>
        /// 获取异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private IActionResult getException(object data, MyException e)
        {
            var tt = e.GetExceptionResult();
            return Json(new JsonResults(tt.resultStatus, tt.resultMsg, data));
        }

    }
}
